package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController("adminSetmealController")
@Slf4j
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 根据ID查询数据
     * @Param id
     * @return
     * */
    @ApiOperation("根据ID查询套餐数据")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("套餐id{}：",id);
        SetmealVO  dishes = setmealService.getByIdWithSetmeal(id);
        return Result.success(dishes);
    }

    /**
     * 新增数据
     * @Param setmealDTO
     * @return
     * */
    @PostMapping
    @ApiOperation("新增套餐数据")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐数据：{}",setmealDTO);
        setmealService.saveWithSetmeal(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询套餐数据
     * @Param setmealPageQueryDTO
     * @return
     * */
    @ApiOperation("分页查询套餐数据")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询套餐数据：{}",setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 套餐起售停售修改
     * @Param status
     * @param id
     * @return
     * */
    @ApiOperation("修改套餐起售状态")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result StartOrStand(@PathVariable Integer status,Long id){
        log.info("套餐：{}，售停状态：{}",id,status);
        setmealService.StartOrStand(status,id);
        return Result.success();
    }

    /**
     * 修改套餐数据
     * @Param setmealDTO
     * @return
     * */
    @ApiOperation("修改套餐信息")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐数据：{}",setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 删除套餐数据
     * @Param setmealDTO
     * @return
     * */
    @ApiOperation("删除套餐信息")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteByIds(@RequestParam List<Long> ids){
        log.info("删除数据：{}",ids);
        setmealService.deleteByIds(ids);
        return Result.success();
    }

}
